home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / Turbo Pascal V7.0 / TVFM.ZIP / FILEFIND.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  6.4 KB  |  267 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision File Manager Demo               }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit FileFind;
  9.  
  10. interface
  11.  
  12. procedure BeginSearch;
  13.  
  14. implementation
  15.  
  16. uses Drivers, Objects, Views, Dialogs, App, Dos, Equ, Globals, DragDrop,
  17.   MsgBox;
  18.  
  19. type
  20.   TMaskStr = string[12];
  21.  
  22.   TSearchCriteria = record
  23.     Mask: TMaskStr;         { mask to match against }
  24.     StartDir: PathStr;
  25.   end;
  26.  
  27.   PStackEntry = ^TStackEntry;
  28.   TStackEntry = record
  29.     Search: SearchRec;
  30.     Dir: PString;
  31.     Prev: PStackEntry;
  32.     First: Boolean;
  33.     DoneWithFiles: Boolean;
  34.   end;
  35.  
  36.   TCountRec = record
  37.     FileCount: Longint;
  38.     DirCount: Longint;
  39.   end;
  40.  
  41.   PFilesBox = ^TFilesBox;
  42.   TFilesBox = object(TListBox)
  43.     function GetText(Item: Integer; MaxLen: Integer): String; virtual;
  44.   end;
  45.  
  46.   PSearchDialog = ^TSearchDialog;
  47.   TSearchDialog = object(TDialog)
  48.     Mask: TMaskStr;
  49.     Count: TCountRec;
  50.     Stack: PStackEntry;
  51.     Button: PButton;
  52.     Params: PParamText;
  53.     FilesBox: PFilesBox;
  54.     constructor Init(var Criteria: TSearchCriteria);
  55.     destructor Done; virtual;
  56.     procedure HandleEvent(var Event: TEvent); virtual;
  57.     function GetNextFile: PathStr;
  58.     procedure DisposeStack;
  59.     procedure ChangeButton;
  60.   end;
  61.  
  62. { TFilesBox }
  63. function TFilesBox.GetText(Item: Integer; MaxLen: Integer): String;
  64. begin
  65.   if Item < List^.Count then GetText := PString(List^.At(Item))^
  66.   else GetText := '';
  67. end;
  68.  
  69. { TSearchDialog }
  70. constructor TSearchDialog.Init(var Criteria: TSearchCriteria);
  71. var
  72.   R: TRect;
  73.   P: PView;
  74.   vSB, hSB: PScrollBar;
  75.   Static: String;
  76.   TextData: array[0..1] of Pointer;
  77. begin
  78.   R.Assign(0,0,60,18);
  79.   inherited Init(R, 'File Search');
  80.   Options := Options or ofCentered;
  81.  
  82.   TextData[0] := @Criteria.Mask;
  83.   TextData[1] := @Criteria.StartDir;
  84.   FormatStr(Static,
  85.     'Search Mask         : %s'#13'Starting from       : %s', TextData);
  86.   R.Assign(2,2,58,4);
  87.   P := New(PStaticText, Init(R, Static));
  88.   Insert(P);
  89.  
  90.   R.Assign(2,4,30,6);
  91.   Params := New(PParamText, Init(R,
  92.     'Files found         : %d'#13'Directories searched: %d', 2));
  93.   Insert(Params);
  94.   Params^.SetData(Count);
  95.  
  96.   R.Assign(57,8,58,14);
  97.   vSB := New(PScrollBar, Init(R));
  98.   Insert(vSB);
  99.  
  100.   R.Assign(2,8,57,14);
  101.   FilesBox := New(PFilesBox, Init(R, 1, vSB));
  102.   FilesBox^.NewList(New(PTextCollection, Init(20,5)));
  103.   Insert(FilesBox);
  104.   R.Assign(2,7,20,8);
  105.   Insert(New(PLabel, Init(R, '~F~iles list', FilesBox)));
  106.  
  107.   R.Assign(0,15,10,17);
  108.   Button := New(PButton, Init(R, '~C~ancel', cmStopSearch,
  109.     bfDefault));
  110.   Button^.Options := Button^.Options or ofCenterX;
  111.   Insert(Button);
  112.  
  113.   Mask := Criteria.Mask;
  114.  
  115.   { initialize the first entry on the stack }
  116.   New(Stack);
  117.   with Criteria do
  118.     if StartDir[Length(StartDir)] = '\' then Dec(StartDir[0]);
  119.   Stack^.Dir := NewStr(Criteria.StartDir);
  120.   Stack^.Prev := nil;
  121.   Stack^.First := True;
  122.   Stack^.DoneWithFiles := False;
  123. end;
  124.  
  125. procedure TSearchDialog.DisposeStack;
  126. var
  127.   SE: PStackEntry;
  128. begin
  129.   if Stack <> nil then
  130.   repeat
  131.     SE := Stack^.Prev;
  132.     DisposeStr(Stack^.Dir);
  133.     Dispose(Stack);
  134.     Stack := SE;
  135.   until Stack = nil;
  136. end;
  137.  
  138. destructor TSearchDialog.Done;
  139. begin
  140.   DisposeStack;
  141.   FilesBox^.NewList(nil);
  142.   inherited Done;
  143. end;
  144.  
  145. function TSearchDialog.GetNextFile: PathStr;
  146. begin
  147.   with Stack^ do
  148.   begin
  149.     if First then
  150.     begin
  151.       First := False;
  152.       FindFirst(Dir^ + '\' + Mask, AnyFile, Search);
  153.     end
  154.     else
  155.       FindNext(Search);
  156.     if DosError = 0 then GetNextFile := Search.Name
  157.     else GetNextFile := '';
  158.   end;
  159. end;
  160.  
  161. procedure TSearchDialog.HandleEvent(var Event: TEvent);
  162. var
  163.   NextItem: PathStr;
  164.   PopStack: Boolean;
  165.   SE: PStackEntry;
  166.   FileName: FNameStr;
  167. begin
  168.   inherited HandleEvent(Event);
  169.   if (Event.What = evCommand) and (Event.Command = cmStopSearch) then
  170.   begin
  171.     DisposeStack;
  172.     ChangeButton;
  173.     ClearEvent(Event);
  174.   end;
  175.   if (Event.What = evBroadcast) and (Event.Command = cmClose) then
  176.   begin
  177.     Event.What := evCommand;
  178.     Event.InfoPtr := @Self;
  179.     PutEvent(Event);
  180.     ClearEvent(Event);
  181.   end;
  182.   if (Event.What = evIdle) and (Stack <> nil) then
  183.   begin
  184.     PopStack := False;
  185.     if Stack^.DoneWithFiles then
  186.     begin
  187.       if Stack^.First then
  188.       begin
  189.         Stack^.First := False;
  190.         FindFirst(Stack^.Dir^ + '\*.', Directory, Stack^.Search);
  191.         while (DosError = 0) and (Stack^.Search.Name[1] = '.') do
  192.           FindNext(Stack^.Search);
  193.       end
  194.       else
  195.         FindNext(Stack^.Search);
  196.       if DosError <> 0 then PopStack := True
  197.       else
  198.       begin   { make a new stack entry }
  199.         New(SE);
  200.         SE^.Prev := Stack;
  201.         SE^.First := True;
  202.         SE^.Dir := NewStr(Stack^.Dir^ + '\' + Stack^.Search.Name);
  203.         SE^.DoneWithFiles := False;
  204.         Stack := SE;
  205.       end;
  206.     end
  207.     else  { not DoneWithFiles }
  208.     begin
  209.       NextItem := GetNextFile;
  210.       if NextItem <> '' then
  211.       begin
  212.         FileName := Stack^.Dir^ + '\' + NextItem;
  213.         FilesBox^.List^.Insert( NewStr(FileName) );
  214.         FilesBox^.SetRange(FilesBox^.List^.Count);
  215.         FilesBox^.FocusItem(FilesBox^.List^.Count);
  216.         Inc(Count.FileCount);
  217.         Params^.SetData(Count);
  218.       end
  219.       else
  220.       begin
  221.         Stack^.DoneWithFiles := True;
  222.         Stack^.First := True;
  223.       end;
  224.     end;
  225.     if PopStack then
  226.     begin
  227.       SE := Stack^.Prev;
  228.       DisposeStr(Stack^.Dir);
  229.       Dispose(Stack);
  230.       Inc(Count.DirCount);
  231.       Params^.SetData(Count);
  232.       Stack := SE;
  233.       if Stack = nil then ChangeButton;  { done searching }
  234.     end;
  235.   end;
  236. end;
  237.  
  238. procedure TSearchDialog.ChangeButton;
  239. var
  240.   R: TRect;
  241. begin
  242.   R.Assign(0,Button^.Origin.Y,11,Button^.Origin.Y + 2);
  243.   Dispose(Button, Done);
  244.   Button := New(PButton, Init(R, '~C~lose', cmClose, bfBroadcast));
  245.   Button^.Options := Button^.Options or ofCenterX;
  246.   Insert(Button);
  247. end;
  248.  
  249.  
  250. procedure BeginSearch;
  251. var
  252.   D: PDialog;
  253.   XFer: TSearchCriteria;
  254. begin
  255.   D := PDialog(RezFile.Get('SearchDialog'));
  256.   XFer.Mask := '*.*';
  257.   GetDir(0, XFer.StartDir);
  258.   if Application^.ExecuteDialog(D, @XFer) = cmOK then
  259.   begin
  260.     D := New(PSearchDialog, Init(XFer));
  261.     Desktop^.Insert(D);
  262.   end;
  263. end;
  264.  
  265. end.
  266.  
  267.